package com.ruoyi.system.service.impl;

import com.ruoyi.system.domain.*;
import com.ruoyi.system.mapper.HomeMapper;
import com.ruoyi.system.mapper.MaterialMapper;
import com.ruoyi.system.mapper.WarehouseMapper;
import com.ruoyi.system.service.HomeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.DateFormat;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Service
public class HomeServiceImpl implements HomeService {
    @Autowired
    private HomeMapper homeMapper;


    @Resource
    private MaterialMapper materialMapper;

    @Autowired
    private WarehouseMapper warehouseMapper;

    @Override
    public Integer selectprojectsAll() {
        return homeMapper.selectprojectsAll();
    }

    @Override
    public Integer selectprojectComplete() {
        return homeMapper.selectprojectComplete();
    }

    @Override
    public Integer selectprojectUnfinished() {
        return homeMapper.selectprojectUnfinished();
    }

    //计算总金额
    @Override
    public List chart() {
        Warehouse warehouse = new Warehouse();
        List<ChartVo> list1=new ArrayList<>();
        List<Warehouse> list= warehouseMapper.selectWarehouseList(warehouse);
        List<String> wares = materialMapper.sreturnWares();
        Double sum=0D;
        for (int i = 0; i <list.size() ; i++) {
            if(null!=homeMapper.selectSumMat(list.get(i).getwId())){
                Double moeny=homeMapper.selectSumMat(list.get(i).getwId());
                sum=sum+moeny;
            }
        }
        for (int i = 0; i < list.size(); i++) {
            ChartVo chartVo=new ChartVo();
            chartVo.setWname(wares.get(i));
            if(null!=homeMapper.selectSumMat(list.get(i).getwId())){
                Double moeny=homeMapper.selectSumMat(list.get(i).getwId());
                double v = moeny / sum;
                chartVo.setPercentage(v*100);
            }
            list1.add(chartVo);
        }
        return list1;
    }

    @Override
    public BarChartVo BarChart(String recordtime) {
        String time=null;
        BarChartVo barChartVo = new BarChartVo();
        DecimalFormat df = new DecimalFormat("######0.00");
        df.setRoundingMode(RoundingMode.HALF_UP);
        //拼接日期字符串
        for (int i = 1; i <= 12; i++) {
            if (i<10){
                time=recordtime+"-0"+i;
            }else {
                time=recordtime+"-"+i;
            }
            //总入库金额
            Double warehousing=homeMapper.selectWarehousing(time);
            //总出库金额
            Double selectstockout=homeMapper.selectStockout(time);
            //判断是否为空  不为空添加
            if(warehousing!=null){
                barChartVo.getWarehousing().add(Double.valueOf(df.format(new BigDecimal(warehousing+""))));
            }else {
                barChartVo.getWarehousing().add(0D);
            }
            if (selectstockout!=null){
                barChartVo.getStockout().add(Double.valueOf(df.format(new BigDecimal(selectstockout+""))));
            }else {
                barChartVo.getStockout().add(0D);
            }
        }
        return barChartVo;
    }

    @Override
    public Balance selectBalance(String time) {
        DecimalFormat def = new DecimalFormat("######0.00");
        def.setRoundingMode(RoundingMode.HALF_UP);
        DateFormat df = new SimpleDateFormat("yyyy-MM");
        Balance balance = new Balance();
        try {
            //获取上个月的时间
            Date date= df.parse(time);
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            calendar.add(Calendar.MONTH, -1);
            String format = df.format(calendar.getTime());
            //上月结余
            Double sum = homeMapper.selectBalance(format);
            //甲供出库
            Double supplyOut=homeMapper.selectSupplyOut(time);
            //乙购出库
            Double purchaseOut=homeMapper.selectPurchaseOut(time);
            //甲供入库
            Double supplyDelivery=homeMapper.selectSupplyDelivery(time);
            //乙购入库
            Double purchaseDelivery=homeMapper.selectPurchaseDelivery(time);
            //本期结余
            Double currentBalance=homeMapper.selectCurrentBalance(time);
            //判空
            if (null==sum){
                sum=0D;
            }
            if (null==supplyOut){
                supplyOut=0D;
            }
            if (null==purchaseOut){
                purchaseOut=0D;
            }
            if (null==supplyDelivery){
                supplyDelivery=0D;
            }
            if (null==purchaseDelivery){
                purchaseDelivery=0D;
            }
            if (null==currentBalance){
                currentBalance=0D;
            }
            Double aDouble = Double.valueOf(def.format(new BigDecimal(sum+"")));
            Double aDouble1 = Double.valueOf(def.format(new BigDecimal(supplyOut+"")));
            Double aDouble2 = Double.valueOf(def.format(new BigDecimal(purchaseOut+"")));

            Double aDouble3 = Double.valueOf(def.format(new BigDecimal(supplyDelivery+"")));
            Double aDouble4 = Double.valueOf(def.format(new BigDecimal(purchaseDelivery+"")));
            Double aDouble5 = Double.valueOf(def.format(new BigDecimal(currentBalance+"")));
            //赋值
            balance.setBalance(aDouble);
            balance.setSupplyOut(aDouble1);
            balance.setPurchaseOut(aDouble2);
            balance.setSupplyDelivery(aDouble3);
            balance.setPurchaseDelivery(aDouble4);
            balance.setCurrentBalance(aDouble5);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return balance;
    }

    @Override
    public Integer insertBalance(Balance balance) {
        return homeMapper.insertBalance(balance);
    }

    @Override
    public CountReq countReq() {
        CountReq countReq = new CountReq();
        countReq.setSum(homeMapper.selectCountReq());
        countReq.setRequisitions(homeMapper.selectReq());
        return countReq;
    }

    @Override
    public CountPur countPur() {
        CountPur countPur = new CountPur();
        countPur.setSum(homeMapper.selectCountPur());
        countPur.setRequisitions(homeMapper.selectPur());
        return countPur;
    }

    @Override
    public List<PaymentUnit> payment() {

        return homeMapper.selectPayment();
    }

    @Override
    public String remark() {

        return homeMapper.remark();

    }

    @Override
    public int addremark(String remark) {
        return homeMapper.addremark(remark);
    }
}
